package util
{
	//影片剪辑按钮
	
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	public class MovieClipButton extends EventDispatcher
	{
		public var movieClip:MovieClip = null;
		private var frameMouseUp:uint = 4;
		private var frameMouseDown:uint = 3;
		private var frameMouseOut:uint = 1;
		private var frameMouseOver:uint = 2;
		private var frameEnabled:uint = 1;
		private var frameDisenabled:uint = 5;
		public function MovieClipButton(mc:MovieClip)
		{
			movieClip = mc;
			movieClip.buttonMode = true;
			movieClip.gotoAndStop(frameEnabled);
			movieClip.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			movieClip.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			movieClip.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			movieClip.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			movieClip.addEventListener(MouseEvent.CLICK, onClick);
		}
		private function onMouseUp(evt:MouseEvent):void
		{
			movieClip.gotoAndStop(frameMouseUp);
		}
		private function onMouseDown(evt:MouseEvent):void
		{
			movieClip.gotoAndStop(frameMouseDown);
		}
		private function onMouseOut(evt:MouseEvent):void
		{
			this.dispatchEvent(evt);
			if(!enable) return;
			movieClip.gotoAndStop(frameMouseOut);
		}
		private function onMouseOver(evt:MouseEvent):void
		{
			movieClip.gotoAndStop(frameMouseOver);
			this.dispatchEvent(evt);
		}
		private function onClick(evt:MouseEvent):void
		{
			this.dispatchEvent(evt);
		}
		public function set visible(v:Boolean):void
		{
			movieClip.visible = v;
		}
		public function get visible():Boolean
		{
			return movieClip.visible;
		}
		public function set enable(e:Boolean):void
		{
			movieClip.mouseEnabled = e;
			movieClip.gotoAndStop(e ?  frameEnabled : frameDisenabled);
		}
		public function get enable():Boolean
		{
			return movieClip.mouseEnabled == frameEnabled;
		}
		public function set buttonMode(b:Boolean):void
		{
			movieClip.buttonMode = b;
		}
	}
}